package com.imooc.demo.service.impl;

import com.imooc.demo.dao.DeviceInfoDao;
import com.imooc.demo.entity.DeviceInfo;
import com.imooc.demo.service.DeviceInfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DeviceInfoServiceImpl implements DeviceInfosService {
    @Autowired
    private DeviceInfoDao deviceInfoDao;


    @Override
    public List<DeviceInfo> getDeviceInfos() {
        return deviceInfoDao.getDeviceInfos();
    }

    @Override
    public List<DeviceInfo> queryDeviceInfoByP(String serialNo) {

        return deviceInfoDao.queryDeviceInfoByP(serialNo);
    }

    @Override
    public List<DeviceInfo> queryTopDeviceInfoByP(String serialNo) {
        List<DeviceInfo> deviceInfos = deviceInfoDao.queryTopDeviceInfoByP(serialNo);
        if (deviceInfos != null) {
            for (DeviceInfo info : deviceInfos) {
                updateDeviceInfo(info);
            }
        }

        return deviceInfos;
    }


    @Transactional
    @Override
    public boolean addDeviceInfo(DeviceInfo deviceInfo) {
        // 空值判断，主要是判断areaName不为空

        // 设置默认值

        try {
            boolean effectedNum = deviceInfoDao.addDeviceInfo(deviceInfo);
            if (effectedNum) {
                return true;
            } else {
                throw new RuntimeException("添加设备信息失败!");
            }
        } catch (Exception e) {
            throw new RuntimeException("添加设备信息失败:" + e.toString());
        }

    }

    @Transactional
    @Override
    public boolean updateDeviceInfo(DeviceInfo deviceInfo) {

        try {
            // 空值判断，主要是areaId不为空
            if (deviceInfo.pId != null) {
                // 设置默认值
                deviceInfo.request_time = new Date();
                try {
                    // 更新区域信息
                    boolean effectedNum = deviceInfoDao.updateDeviceInfo(deviceInfo);
                    if (effectedNum) {
                        return true;
                    } else {
                        throw new RuntimeException("更新设备信息失败!");
                    }
                } catch (Exception e) {
                    throw new RuntimeException("更新设备信息失败:" + e.toString());
                }
            } else {
                throw new RuntimeException("设备信息不能为空！");
            }
        } catch (Exception e) {
            throw new RuntimeException("更新设备信息失败:" + e.toString());
        }

    }


    @Transactional
    @Override
    public boolean deleteDeviceInfo(String phoneNo) {

        if (phoneNo != null && phoneNo != "") {
            try {
                // 删除设备信息
                boolean effectedNum = deviceInfoDao.deleteDeviceInfo(phoneNo);
                if (effectedNum) {
                    return true;
                } else {
                    throw new RuntimeException("删除设备信息失败!");
                }
            } catch (Exception e) {
                throw new RuntimeException("删除设备信息失败:" + e.toString());
            }
        } else {
            throw new RuntimeException("设备Id不能为空！");
        }


    }
}
